package com.faceCaptor.util;

import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;

public class ImageUtil {

	public static Image zoomImage(BufferedImage bufImg, int w, int h) {
		double wr = 0, hr = 0;
		Image Itemp = bufImg.getScaledInstance(w, h, Image.SCALE_SMOOTH);// 设置缩放目标图片模板

		wr = w * 1.0 / bufImg.getWidth(); // 获取缩放比例
		hr = h * 1.0 / bufImg.getHeight();

		AffineTransformOp ato = new AffineTransformOp(AffineTransform.getScaleInstance(wr, hr), null);
		Itemp = ato.filter(bufImg, null);
		return Itemp;
	}

	public static ImageData zoomImage(ImageData data, int w, int h) {
		return data.scaledTo(w, h);

	}

	public static byte[] imageToByte(org.eclipse.swt.graphics.Image image) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();

		ImageLoader imageLoader = new ImageLoader();
		imageLoader.data = new ImageData[] { image.getImageData() };
		imageLoader.save(baos, image.type);
		byte[] imageByteArray = baos.toByteArray();
		try {
			baos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return imageByteArray;
	}

	public byte[] image2byte(String path) {
		byte[] data = null;
		FileInputStream input = null;
		try {
			input = new FileInputStream(new File(path));
			ByteArrayOutputStream output = new ByteArrayOutputStream();
			byte[] buf = new byte[1024];
			int numBytesRead = 0;
			while ((numBytesRead = input.read(buf)) != -1) {
				output.write(buf, 0, numBytesRead);
			}
			data = output.toByteArray();
			output.close();
			input.close();
		} catch (FileNotFoundException ex1) {
			ex1.printStackTrace();
		} catch (IOException ex1) {
			ex1.printStackTrace();
		}
		return data;
	}

	public static void main(String args[]) {
//		File file = new File("D:\\Users\\wangwb\\workspace\\FaceCaptor\\src\\log4j.properties");
//		String[] filenames = file.getName().split("\\.");
//		System.out.println(filenames[0]);
		
	 int[] index = new int[]{0};
	 for(int i=0;i<20;i++){
		 
		 System.out.println(index[0]);
		 
		 if(index[0]>=9){
			 index[0] = index[0]%9;
		 }else{
			 index[0]++;
		 }
		 
		 
	 }
//	 System.out.println(index[0]);
	}
}
